﻿using _1._8._2015_SHero_Auth.Data;
using _1._8._2015_SHero_Auth.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity.Migrations;
using _1._8._2015_SHero_Auth.Data.Models;

namespace _1._8._2015_SHero_Auth.Controllers
{
    [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {
        // GET: Admin
        
        public ActionResult Index()
        {
            ApplicationDbContext db = new ApplicationDbContext();
            SHerosViewModel model = new SHerosViewModel();

            model.SHeros = db.SHeros.Select(sh => new SHeroViewModel
            {
                Name = sh.Name,
                Origin = sh.Origin,
                SuperPower = sh.SuperPower,
                PowerSource = sh.PowerSource,
                SHeroId = sh.SHeroId
            }).ToList();

            return View(model);
        }

        public ActionResult CreateSHero()
        {
            SHeroViewModel model = new SHeroViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult CreateSHero(SHeroViewModel model)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            db.SHeros.AddOrUpdate(sh => new {sh.Name, sh.Origin, sh.PowerSource, sh.SuperPower},   
            new SHero
            {
                Name = model.Name,
                Origin = model.Origin,
                PowerSource = model.PowerSource,
                SuperPower = model.SuperPower
            });
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        
        public ActionResult UpdateSHero(int id)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            SHeroViewModel model = new SHeroViewModel();
            SHero sHero =  db.SHeros.FirstOrDefault(sh => sh.SHeroId == id);

            model.Name = sHero.Name;
            model.Origin = sHero.Origin;
            model.SHeroId = sHero.SHeroId;
                        
            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateSHero(SHeroViewModel model, int id)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            SHero sHero = db.SHeros.FirstOrDefault(sh => sh.SHeroId == id);

            sHero.Name = model.Name;
            sHero.Origin = model.Origin;
            sHero.PowerSource = model.PowerSource;
            sHero.SuperPower = model.SuperPower;

            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult DeleteSHero(int id)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            SHero sHero = db.SHeros.FirstOrDefault(sh => sh.SHeroId == id);
            db.SHeros.Remove(sHero);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

    }
}